package com.imooc.news_manager_project.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @project: news_manager_project
 * @author: 13965
 * @date：2023/9/28 14:58
 * @description:
 */
@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    /**
     * @description: String set操作
     * @author: 13965
     * @date: 2023/6/19 15:07
     * @param: key: 键
     * @param: value: 值
     * @return: boolean 成功返回 true 失败返回 false
     * @version: 1.0
     **/
    public boolean set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @description: String 设置键值过期时间
     * @author: 13965
     * @date: 2023/6/19 15:43
     * @param: key: 键
     * @param: value: 值
     * @param: time: 过期时间
     * @return: boolean
     * @version: 1.0
     **/
    public boolean set(String key, Object value, long time) {
        try {
            if (time > 0) {
                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            } else {
                set(key, value);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @description: String get
     * @author: 13965
     * @date: 2023/6/19 15:40
     * @param: key: 键值
     * @return: java.lang.Object
     * @version: 1.0
     **/
    public Object get(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }

    /**
     * @description: 判断Redis中是否存在对应的key
     * @author: 13965
     * @date: 2023/6/19 15:45
     * @param: key: 键
     * @return: boolean  true 存在，false 不存在
     * @version: 1.0
     **/
    public boolean hasKey(final String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * @description: 指定key失效时间
     * @author: 13965
     * @date: 2023/6/19 15:47
     * @param: key: 键
     * @param: time: 失效时间
     * @return: boolean
     * @version: 1.0
     **/
    public boolean expire(String key, long time) {
        try {
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @description: 根据 key 获取剩余过期时间
     * @author: 13965
     * @date: 2023/6/19 15:47
     * @param: key:  键
     * @return: long 剩余过期是时间
     * @version: 1.0
     **/
    public long getExpire(String key) {
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    /**
     * @description: 删除指定的key
     * @author: 13965
     * @date: 2023/6/21 10:29
     * @param: key:
     * @return: boolean
     * @version: 1.0
     **/
    public boolean remove(String key) {
        if (hasKey(key)) {
            Boolean delete = redisTemplate.delete(key);
            return delete;
        }
        return false;
    }


    /**
     * @description: 对指定key自增1，并返回自增后的数值
     * @author: 13965
     * @date: 2023/9/17 11:00
     * @param: key:
     * @return: java.lang.Long
     * @version: 1.0
     **/
    public Long incrementValue(String key) {
        Long num = redisTemplate.opsForValue().increment(key, 1);
        return num;
    }

    /**
     * set中增加元素，支持一次增加多个元素，逗号分隔即可，结果返回添加的个数
     *
     * @param key
     * @param value
     * @return
     */
    public Long addSet(String key, Object... value) {
        Long size = null;
        try {
            size = redisTemplate.opsForSet().add(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return size;
        }
        return size;
    }

    /**
     * set中移除指定元素
     *
     * @param key
     * @param value
     * @return
     */
    public Long removeSet(String key, Object value) {
        Long size = null;
        try {
            size = redisTemplate.opsForSet().remove(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return size;
        }
        return size;
    }

    /**
     * 计算set集合大小
     *
     * @param key
     * @return
     */
    public Long countSet(String key) {
        Long size = null;
        try {
            size = redisTemplate.opsForSet().size(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }

    /**
     * 判断set中是否存在某元素
     *
     * @param key
     * @param value
     * @return
     */
    public Boolean hasMemberSet(String key, Object value) {
        Boolean exist = false;
        try {
            exist = redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return exist;
    }

}
